//
//  STDSColorGrainEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/3.
//

#ifndef STDSColorGrainEffect_h
#define STDSColorGrainEffect_h


#include "STDSBaseEffect.hpp"

class STDSColorGrainEffect: public STDSBaseEffect {
public:
    STDSColorGrainEffect() {
        mEffectType = STDSEffectTypeColorGrain;
    }
    ~STDSColorGrainEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        if (strcmp("grain", name) == 0) {
            mGrain = *floatValue;
        }
    }
    
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mColorGrainProgram == nullptr) {
            mColorGrainProgram = mContext->getProgram(kShaderSRCColorGrain);
        }
        return mColorGrainProgram;
    }
    
    void draw() override {
        auto program = getEffectProgram();
        if (program == nullptr) {
            return;
        }
        if (mTexture == nullptr) {
            return;
        }
        
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("inputImageTexture"), 0);
        program->setUniform(program->getUniform("grain"), mGrain);
        
        program->setUniform2f(program->getUniform("size"), mTexture->getWidth(), mTexture->getHeight());
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    float mGrain{0.0};
    shared_ptr<STDSShaderProgram> mColorGrainProgram{nullptr};
};

#endif /* STDSColorGrainEffect_h */
